package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;
import com.sun.javaws.jnl.RContentDesc;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    private int a;

    /**
     * 根据userId businessId查询购物车
     * @param userId
     * @param businessId
     * @return cart数组
     * @throws SQLException
     */
    @Override
    public List<Cart> listCart(String userId, Integer businessId) throws SQLException {
        List<Cart> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer();
        String sql1 = "select * from cart c left join business b on c.businessId = b.businessId left join food f on c.foodId=f.foodId where c.userId=?";
        String sql2 = " and c.businessId=?";
        sql.append(sql1);
        try {
            //获取连接
            con = DBUtil.getConnection();
            if(businessId != 0){ // 查询时有businessId
                sql = sql.append(sql2);
                pst = con.prepareStatement(String.valueOf(sql));
                pst.setString(1,userId);
                pst.setInt(2,businessId);
            }else{ // 只根据userId查询 businessId=0
                pst = con.prepareStatement(String.valueOf(sql));
                pst.setString(1,userId);
            }
            rs = pst.executeQuery();
            while(rs.next()) {
                Cart cart = new Cart();
                Business business = new Business();
                Food food = new Food();
                cart.setCartId(rs.getInt("cartId"));
                cart.setFoodId(rs.getInt("foodId"));
                cart.setBusinessId(rs.getInt("businessId"));
                cart.setUserId(rs.getString("userId"));
                cart.setQuantity(rs.getInt("quantity"));
                // 多表查询 将查到的business数据添加进business对象中
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                // 多表查询 将查到的food数据添加进food对象中
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));
                // 将business对象添加到cart中
                cart.setBusiness(business);
                // 将food对象添加到cart中
                cart.setFood(food);
                // 将cart对象添加到list
                list.add(cart);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }

    @Override
    public int saveCart(Cart cart) throws SQLException{
        String sql = "insert into cart(foodId,businessId,userId,quantity) values(?,?,?,?)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,cart.getFoodId());
            pst.setInt(2,cart.getBusinessId());
            pst.setString(3,cart.getUserId());
            pst.setInt(4,cart.getQuantity());
            a = pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs,pst);
        }
        return a;
    }

    @Override
    public int updateCart(Cart cart) throws SQLException{
        String sql = "update cart set quantity=? where userId=? and businessId=? and foodId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, cart.getQuantity());
            pst.setString(2, cart.getUserId());
            pst.setInt(3, cart.getBusinessId());
            pst.setInt(4, cart.getFoodId());
            a = pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs,pst);
        }
        return a;
    }

    @Override
    public int removeCart(String userId, Integer businessId, Integer foodId) throws SQLException {
        StringBuffer sql = new StringBuffer();
        String sql1 = "delete from cart where userId=? and businessId=?";
        String sql2 = " and foodId=?";
        sql.append(sql1);
        try {
            //获取连接
            con = DBUtil.getConnection();
            if (foodId != 0) { // 查询时有foodId
                sql.append(sql2);
                pst = con.prepareStatement(String.valueOf(sql));
                pst.setString(1, userId);
                pst.setInt(2, businessId);
                pst.setInt(3, foodId);
            } else { // 查询时不需要foodId foodId=0
                pst = con.prepareStatement(String.valueOf(sql));
                pst.setString(1, userId);
                pst.setInt(2, businessId);
            }
            a = pst.executeUpdate();
        } finally {
            DBUtil.close(rs,pst);
        }
        return a;
    }


}
